package com.stox.core.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.stox.core.dao.mapper.IndexRowMapper;
import com.stox.core.model.Index;
import com.stox.core.model.exception.StoxException;
import com.stox.design.DatabaseConfigurer;

public class IndexDao extends AbstractDao implements DatabaseConfigurer{

	private IndexRowMapper indexRowMapper;
	
	@Override
	public int getOrder() {
		return 0;
	}
	
	@Override
	public Object invoke(List<String> payload) {
		if(!payload.contains("INDEX_")){
			try {
				getConnection().createStatement().execute(IndexRowMapper.createSql);
				getConnection().createStatement().execute(IndexRowMapper.createIndexSql);
			} catch (SQLException e) {
				throw new StoxException(e);
			}
		}
		
		if(!payload.contains("INDEX_SCRIP")){
			try {
				getConnection().createStatement().execute(IndexRowMapper.createScripMappingSql);
			} catch (SQLException e) {
				throw new StoxException(e);
			}
		}
		return null;
	}
	
	
	public List<Index> findByExchangeCode(String exchangeCode) throws SQLException{
		PreparedStatement ps = getConnection().prepareStatement(IndexRowMapper.selectSql+" where exchangeCode=?");
		ps.setString(1, exchangeCode);
		return indexRowMapper.map(ps.executeQuery());
	}

	public IndexRowMapper getIndexRowMapper() {
		return indexRowMapper;
	}

	public void setIndexRowMapper(IndexRowMapper indexRowMapper) {
		this.indexRowMapper = indexRowMapper;
	}
	
	
}
